<?php

namespace app\admin\model\yunfood;

use addons\yunfood\controller\Fund;
use addons\yunfood\model\FundRecord;
use addons\yunfood\model\OrderItem;
use think\Exception;

/**
 * 退款订单
 */
class OrderRefund extends \addons\yunfood\model\OrderRefund
{
    // 自定义初始化
    protected static function init()
    {
        self::beforeWrite(function ($record) {
            $scenario = $record->data['scenario'] ?? null;
            if ($scenario === 'reply') {
                if ($record->status == self::STATUS_WAIT) {
                    $agree = $record['agree'] ?? 1;
                    $record->refund_time = time();
                    $record->status = $agree ? self::STATUS_DONE : self::STATUS_REFUSE;
                    if (! $orderData = $record->orderData) {
                        throw new Exception('订单不存在');
                    }
                    if ($record->order_item_no) {
                        if (! $orderItemData = $record->orderItemData) {
                            throw new Exception('订单商品不存在');
                        }
                        $orderItemData->refund_status = $agree ? OrderItem::REFUND_STATUS_DOWN : OrderItem::REFUND_STATUS_REFUSE;
                        $orderItemData->save();

                        // 全部退款完成
                        if ($agree) {
                            $allRefund = true;
                            if ($orderItemList = $orderData->orderItemListData) {
                                foreach ($orderItemList as $item) {
                                    if ($item->refund_status != OrderItem::REFUND_STATUS_DOWN) {
                                        $allRefund = false;
                                        break;
                                    }
                                }
                            }
                            if ($allRefund) {
                                $orderData->refund_status = Order::REFUND_STATUS_DOWN;
                                $orderData->status = Order::STATUS_HAS_CLOSE;
                                $orderData->save();
                            }
                        }
                        // 余额退款
                        if ($record->refund_balance > 0) {
                            FundUser::amountCounter($record->user_id, FundRecord::ACTION_REFUND,
                                $record->refund_balance, FundRecord::SOURCE_TYPE_ORDER_ITEM, $record->order_item_no);
                        }
                    } else {
                        $orderData->refund_status = $agree ? Order::REFUND_STATUS_DOWN : Order::REFUND_STATUS_REFUSE;
                        if ($agree) {
                            $orderData->status = Order::STATUS_HAS_CLOSE;
                        }
                        $orderData->save();
                        // 余额退款
                        if ($record->refund_balance > 0) {
                            FundUser::amountCounter($record->user_id, FundRecord::ACTION_REFUND,
                                $record->refund_balance, FundRecord::SOURCE_TYPE_ORDER, $record->order_no);
                        }
                    }
                }
            }

            if (! empty($record->data['deleted_at'])) {
                $orderData = $record->orderData;
                if ($record->order_item_no) {
                    if ($orderItemData = $record->orderItemData) {
                        $orderItemData->refund_no = null;
                        $orderItemData->refund_status = OrderItem::REFUND_STATUS_NO;
                        $orderItemData->save();
                    }
                    if ($orderData) {
                        $allRefundNo = true;
                        if ($orderItemList = $orderData->orderItemListData) {
                            foreach ($orderItemList as $item) {
                                if ($item->refund_status != OrderItem::REFUND_STATUS_NO) {
                                    $allRefundNo = false;
                                }
                            }
                        }
                        if ($allRefundNo) {
                            $orderData->refund_status = Order::REFUND_STATUS_NO;
                            $orderData->save();
                        }
                        // 余额退款
                        if ($record->refund_balance > 0) {
                            FundUser::amountCounter($record->user_id, FundRecord::ACTION_REFUND,
                                $record->refund_balance, FundRecord::SOURCE_TYPE_ORDER_ITEM, $record->order_item_no);
                        }
                    }
                } else {
                    if ($orderData) {
                        $orderData->refund_no = null;
                        $orderData->refund_status = Order::REFUND_STATUS_NO;
                        $orderData->save();

                        // 余额退款
                        if ($record->refund_balance > 0) {
                            FundUser::amountCounter($record->user_id, FundRecord::ACTION_REFUND,
                                $record->refund_balance, FundRecord::SOURCE_TYPE_ORDER, $record->order_no);
                        }
                    }
                }
            }
        });
    }
}
